import Layout from '@/layout/index.vue';
import { createRouter, createWebHistory } from 'vue-router';
import { i18n } from '@/i18n'; // 导入全局 i18n 实例

export const routes = [
    {
        path: '/',
        meta: {
            title: 'menu.home',
            icon: '#iconhome',
            hidden: false,
            hasChild: false,
        },
        component: Layout,
        children: [
            {
                path: '/dashboard',
                component: () => import('@/layout/dashboard/index.vue'),
                name: 'dashboard',
                meta: {
                    title: 'menu.home',
                    icon: '#iconhome',
                    hidden: false,
                }
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            title: 'menu.loginPage',
            icon: '',
            hidden: true,
            hasChild: false,
        },
        component: () => import('@/views/login/Login.vue'),
    },
    {
        path: '/ingredient-manage',
        name: 'ingredient-manage',
        meta: {
            title: 'menu.foodManage',
            icon: '#iconingredient',
            hidden: false,
            hasChild: true,
        },
        component: Layout,
        children: [
            {
                path: '/ingredient-manage/ingredient',
                name: 'ingredient',
                component: () => import('@/views/ingredient-manage/ingredient/Ingredient.vue'),
                meta: {
                    title: 'menu.ingredientsList',
                    icon: '#icondashboard',
                    hidden: false,
                }
            },
            {
                path: '/ingredient-manage/ingredient-category',
                name: 'ingredient-category',
                component: () => import('@/views/ingredient-manage/ingredient-category/IngredientCategory.vue'),
                meta: {
                    title: 'menu.foodClassification',
                    icon: '#iconingredientcategory',
                    hidden: false,
                }
            },
        ]
    },
    {
        path: '/cookbook-manage',
        name: 'cookbook-manage',
        meta: {
            title: 'menu.recipeManagement',
            icon: '#iconcookbookmanage',
            hidden: false,
            hasChild: true,
        },
        component: Layout,
        children: [
            {
                path: '/cookbook-manage/cookbook-list',
                name: 'cookbook-list',
                component: () => import('@/views/cookbook-manage/cookbook-list/cookbookList.vue'),
                meta: {
                    title: 'menu.recipeList',
                    icon: '#iconcookbooklist',
                    hidden: false,
                }
            },
            {
                path: '/cookbook-manage/cookbook-category',
                name: 'cookbook-category',
                component: () => import('@/views/cookbook-manage/cookbook-category/cookbookCategory.vue'),
                meta: {
                    title: 'menu.RecipeClassification',
                    icon: '#iconcookbookcategory',
                    hidden: false,
                }
            },
            {
                path: '/cookbook-manage/cookbook-detail',
                name: 'cookbook-detail',
                component: () => import('@/views/cookbook-manage/cookbook-detail/cookbookDetail.vue'),
                meta: {
                    title: 'menu.RecipeDetails',
                    icon: '',
                    hidden: true,
                }
            },
        ]
    },
    {
        path: '/',
        name: 'calendar',
        meta: {
            title: 'menu.CalendarManagement',
            icon: '#iconcalendarmanage',
            hidden: false,
            hasChild: false,
        },
        component: Layout,
        children: [
            {
                path: '/calendar/daily',
                name: 'calendar/daily',
                component: () => import('@/views/cookbook/daily/Daily.vue'),
                meta: {
                    title: 'menu.CalendarManagement',
                    icon: '#iconcalendarmanage',
                    hidden: false,
                }
            },
        ]
    },
    {
        path: '/system',
        name: 'system',
        meta: {
            title: 'menu.systemManagement',
            icon: '#iconsystem',
            hidden: false,
            hasChild: true,
        },
        component: Layout,
        children: [
            {
                path: '/system/user',
                name: 'user',
                component: () => import('@/views/system/user/User.vue'),
                meta: {
                    title: 'menu.userManage',
                    icon: '#iconuser',
                    hidden: false,
                }
            },
            {
                path: '/system/role',
                name: 'role',
                component: () => import('@/views/system/role/Role.vue'),
                meta: {
                    title: 'menu.roleManage',
                    icon: '#iconrole',
                    hidden: false,
                }
            },
            {
                path: '/system/auth',
                name: 'auth',
                component: () => import('@/views/system/auth/authList.vue'),
                meta: {
                    title: 'menu.authManage',
                    icon: '#iconrole',
                    hidden: false,
                }
            },
        ]
    },
    {
        path: '/user/center',
        name: '/user/center',
        meta: {
            title: '个人中心',
            icon: '#iconcalendarmanage',
            hidden: true,
            hasChild: false,
        },
        component: Layout,
        children: [
            {
                path: '/user/center',
                name: 'user-center',
                component: () => import('@/views/user/UserCenter.vue'),
                meta: {
                    title: '个人中心',
                    icon: '#iconcalendarmanage',
                    hidden: true,
                }
            },
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})

// 全局前置守卫：动态设置标题
router.beforeEach((to) => {
    const nearestWithTitle = to.matched
        .slice()
        .reverse()
        .find((r) => r.meta?.title)

    if (nearestWithTitle) {
        // 使用 i18n.global.t() 进行翻译
        document.title = i18n.global.t(nearestWithTitle.meta.title);
    } else {
        document.title = i18n.global.t('page.shijiCloudStack') // 设置默认标题
    }
})

// 全局前置守卫：动态设置标题
router.afterEach((to) => {
    const nearestWithTitle = to.matched
        .slice()
        .reverse()
        .find((r) => r.meta?.title)

    if (nearestWithTitle) {
        // 使用 i18n.global.t() 进行翻译
        document.title = i18n.global.t(nearestWithTitle.meta.title);
    } else {
        document.title = i18n.global.t('page.shijiCloudStack') // 设置默认标题
    }
})

export default router
